﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GeneralClasses;

namespace Interface
{
    public partial class frmShifts : Form
    {
        Form caller;
        Shift shift;

        public frmShifts(Form Caller)
        {
            caller = Caller;

            InitializeComponent();
        }

        public frmShifts(Form Caller, Shift editShift)
        {
            caller = Caller;
            shift = editShift;

            InitializeComponent();
        }

        public void refresh()
        {
            if (shift != null)
            {
                txtShiftName.Text = shift.Name;
                dtpStartHour.Text = Convert.ToString(shift.StartTime.Hour);
                dtpEndHour.Text = Convert.ToString(shift.EndTime.Hour);
            }
        }

        private void btnDepartments_Click(object sender, EventArgs e)
        {
            this.Hide();
            frmDepartments departments = new frmDepartments(this);
            departments.Show();
        }

        private void btnBack_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void frmShifts_FormClosing(object sender, FormClosingEventArgs e)
        {
            caller.Show();
        }

        private void btnAddShift_Click(object sender, EventArgs e)
        {
            if (txtShiftName.Text == "")
            {
                MessageBox.Show("Please enter a name for the shift.");
                txtShiftName.Focus();
            }
            else
            {
                if (shift == null)
                {
                    shift = new Shift();
                }

                DateTime startTime = new DateTime(shift.StartTime.Year, shift.StartTime.Month,
                    shift.StartTime.Day, Convert.ToInt32(dtpStartHour.Text), shift.StartTime.Minute, 
                    shift.StartTime.Second, shift.StartTime.Millisecond);
                DateTime endTime = new DateTime(shift.EndTime.Year, shift.EndTime.Month,
                    shift.EndTime.Day, Convert.ToInt32(dtpEndHour.Text), shift.EndTime.Minute,
                    shift.EndTime.Second, shift.EndTime.Millisecond);
                shift.Name = txtShiftName.Text;
                shift.StartTime = startTime;
                shift.EndTime = endTime;

                if (shift.saveData() > 0)
                {
                    MessageBox.Show("Added shift to database.");
                    this.Close();
                }
                else
                {
                    MessageBox.Show("Problem adding shift to database.");
                    shift = null;
                }
            }
        }

        private void frmShifts_Shown(object sender, EventArgs e)
        {
            refresh();
        }

        private void btnTitles_Click(object sender, EventArgs e)
        {
            this.Hide();
            frmTitles titles = new frmTitles(this);
            titles.Show();
        }

        private void btnSkills_Click(object sender, EventArgs e)
        {
            this.Hide();
            frmSkills skills = new frmSkills(this);
            skills.Show();
        }
    }
}
